package com.thirtydays.frame.util

import android.Manifest
import android.content.Context
import android.os.Build
import com.blankj.utilcode.util.PermissionUtils

/**
 * @创建者：小垚
 * @时间：2021/7/2215:37
 * @描述：申请权限统一处理
 */
object PermissionUtil {
    fun request(context: Context, function: () -> Unit = {}) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!PermissionUtils.isGranted(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                PermissionUtils.permission(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .rationale { _, _ ->
                        XpopHelp.showCenterTip(context, "去开启权限？") {
                            it.dismiss()
                            PermissionUtils.launchAppDetailsSettings()
                        }
                    }
                    .callback { isAllGranted, _, _, _ ->
                        if (!isAllGranted) {
                            XpopHelp.showCenterTip(context, "去开启权限？") {
                                it.dismiss()
                                PermissionUtils.launchAppDetailsSettings()
                            }
                        } else {
                            function()
                        }
                    }
                    .request()
            }else{
                function()
            }
        } else {
            function()
        }
    }
}